package kzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import kzy.common.CustomException;
import kzy.entiy.Category;
import kzy.entiy.Dish;
import kzy.entiy.Employee;
import kzy.entiy.Setmeal;
import kzy.mapper.CategoryMapper;
import kzy.mapper.EmployeeMapper;
import kzy.service.CategoryService;
import kzy.service.DishService;
import kzy.service.EmployeeService;
import kzy.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isNotEmpty;

/**
 * @Author 大溪地
 * @create 2023年05月24日21:29
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

	@Autowired
	DishService  dishService;

	@Autowired
	SetmealService setmealService;


	/**
	 * 删除分类 删除之前要 判断是否能删除
	 * @param id
	 */
	@Override
	public void removeId(Long id) {

//		判断菜品是否有关联要删掉的分类
		//构造条件构造器
		LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		//添加过滤条件
		queryWrapper.eq(Dish::getCategoryId,id);
		int count = dishService.count(queryWrapper);

//		判断套餐是否有关联要删掉的分类
		//构造条件构造器
		LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper<>();
		//添加过滤条件
		queryWrapper2.eq(Setmeal::getCategoryId,id);

		int count2 = setmealService.count(queryWrapper2);
		System.out.println("________________________________________________________");
		System.out.println("count："+count);
		System.out.println("________________________________________________________");
		System.out.println("count2："+count2);
//  判断是否有菜品关联
		if(count>0){
			log.debug("有菜品关联不能删除");
			throw new CustomException("有菜品关联不能删除");
		}
//  判断是否有套餐关联
		else if(count2>0){
			log.debug("有套餐关联不能删除");
			throw new CustomException("有套餐关联不能删除");

		}
//！ Super是一个参考（或说指针）指向他紧邻的父类
// 使用父类 CategoryMapper 删除id
		if(!(super.removeById(id))){
			log.debug("删除失败,原因未知");
			throw new CustomException("删除失败,原因未知");

		}
	}
}
